/**
 * Project Name:kany-project-spring-boot
 * File Name:InternetProtocolController.java
 * Package Name:me.kany.project.controller
 * Date:2016年4月19日上午12:24:48
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.project.spring.boot.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import me.kany.utils.baidu.map.LocationUtils;
import me.kany.utils.iputils.IPLocation;
import me.kany.utils.iputils.IPUtils;

/**
 * ClassName:InternetProtocolController <br/>
 * Function: 主要用来处理Ip的操作. <br/>
 * Date:     2016年4月19日 上午12:24:48 <br/>
 * @author   Jason.Wang
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
@RestController
@RequestMapping("/ip")
public class InternetProtocolController {
	
	/**
	 * 
	 * execute:获取当前访问的真实的Ip信息. <br/>
	 *
	 * @author Jason.Wang
	 * @param request
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping
	public String execute(HttpServletRequest request){
		String ipAddress = IPUtils.getClientIpAddr(request);
		String form = "";
		try {
			IPLocation ipLocation = IPUtils.getIPSeeker(ipAddress);
			form = " 来自："+ ipLocation.getArea() + ipLocation.getCountry();
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject jsonObject = LocationUtils.getLocationByIP(ipAddress);
		if(0 == jsonObject.getIntValue("status")){
			JSONObject content = jsonObject.getJSONObject("content");
			form += "<br/>IP定位地址：" + content.getString("address");
			JSONObject point = content.getJSONObject("point");
			form += String.format("<br />定位坐标，经度(Lat)：%s 纬度(Lat)：%s", point.getString("y"), point.getString("x"));		
		}
		return ipAddress + form;
	}
}
